package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Tourist;
import cn.wolfcode.luowowo.article.domain.TouristDetail;
import cn.wolfcode.luowowo.article.service.ITouristDetailService;
import cn.wolfcode.luowowo.article.service.ITouristService;
import cn.wolfcode.luowowo.comment.domain.TouristComment;
import cn.wolfcode.luowowo.comment.query.TouristCommentQuery;
import cn.wolfcode.luowowo.comment.service.ITouristCommentService;
import cn.wolfcode.luowowo.common.utils.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.web.annotation.GetUserInfoAnnotation;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class TouristController {

    @Reference
    private ITouristService touristService;
    @Reference
    private ITouristDetailService touristDetailService;
    @Reference
    private IUserInfoService userInfoService;
    @Reference
    private ITouristCommentService touristCommentService;

    @RequestMapping("destination/tourist")
    public String tourist(Model model, Long id) {//目的地id
        //根据目的地id查询关联的景点 list  查询必游景点Top5
        List<Tourist> tourists = touristService.queryTop5ByDestId(id);
        if (tourists.size() > 5) {
            tourists = tourists.subList(0, 5);
        }
        model.addAttribute("tourists", tourists);

        return "tourist/list";
    }
    @RequestMapping("tourist/comment")
    public String comment(Model model,@ModelAttribute("qo") TouristCommentQuery qo){
        //Page page = touristCommentService.query(qo);
        List<TouristComment> list =  touristCommentService.getById(qo.getTouristId());
        model.addAttribute("page",list);
        return "tourist/commentTpl";
    }



    @RequestMapping("tourist/detail")
    public String detail(Model model, Long id) {//景点id
        TouristDetail touristDetail = touristDetailService.get(id);
        model.addAttribute("touristDetail",touristDetail);
        return "tourist/detail";
    }
    @RequestMapping("tourist/commentAdd")
    public Object commentAdd(Model model, TouristComment comment, @ModelAttribute("qo") TouristCommentQuery qo,@GetUserInfoAnnotation UserInfo userInfo){

        //用户信息
        comment.setUserId(userInfo.getId());
        comment.setUsername(userInfo.getNickname());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setLevel(userInfo.getLevel());
        touristCommentService.commentAdd(comment);

        model.addAttribute("c",comment);
        List<TouristComment> list =  touristCommentService.getById(qo.getTouristId());
        model.addAttribute("page",list);
        model.addAttribute("floor",touristCommentService.count(comment.getTouristId()) );//当前评论
        return "tourist/commentTpl";
    }
    //评论点赞
    @RequestMapping("tourist/ThumbUp")
    @ResponseBody
    public Object commentThumbUp(String id, @GetUserInfoAnnotation UserInfo userInfo){ //评论id
        boolean a = touristCommentService.commentThumbUp(id,userInfo.getId());
        AjaxResult ajaxResult = new AjaxResult();
        if (a){
            ajaxResult.setMsg("点赞成功");
            return ajaxResult;
        }else {
            ajaxResult.setMsg("取消点赞");
            ajaxResult.setSuccess(false);
            return ajaxResult;
        }

    }
    //回复
    @RequestMapping("commentAdd")
    public String commentAdd(TouristComment comment, Model model, @GetUserInfoAnnotation UserInfo userInfo, int floor){
        comment.setCity(userInfo.getCity());
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setLevel(userInfo.getLevel());
        comment.setUserId(userInfo.getId());
        comment.setUsername(userInfo.getNickname());

        TouristComment c = touristCommentService.commentAdd(comment);
        model.addAttribute("c",c);
        Long count = touristCommentService.count(comment.getTouristId());
        model.addAttribute("floor",count);
        return "tourist/commentTpl";
    }
}
